package com.mytx.zuul.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StreamUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.util.Enumeration;
import java.util.NoSuchElementException;

@SuppressWarnings("Duplicates")
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private byte[] requestBody = null;

    @Override
    public void setAttribute(String name, Object o) {
        super.setAttribute(name, o);
    }

    public MyHttpServletRequestWrapper(HttpServletRequest request) {

        super(request);

    }




    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (requestBody == null) {
            requestBody = new byte[0];
        }
        return new ServletInputStreamWrapper(requestBody);
    }


    @Override
    public int getContentLength() {
        return requestBody.length;
    }

    @Override
    public long getContentLengthLong() {
        return requestBody.length;
    }

    public void setRequestBody(String bodyStr) {
        this.requestBody = bodyStr.getBytes();
    }



}
